package com.ruoyi.web.controller.biz;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.ruoyi.biz.model.excel.StatRiskExcel;
import com.ruoyi.biz.model.req.StatReq;
import com.ruoyi.biz.model.excel.StatCzExcel;
import com.ruoyi.biz.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author tianl
 * @date 2025/1/3
 * @description:
 */
@RestController
@RequestMapping("/biz/statApi")
public class StatisticController extends BaseController {
    @Autowired
    private CalendarService calendarService;
    @Autowired
    private StatProjectService statProjectService;
    @Autowired
    private StatAuditService statAuditService;
    @Autowired
    private StatRiskService statRiskService;
    @Autowired
    private StatExportService statExportService;
    @PostMapping(value = "/exportRisk")
    public void exportRisk(Integer year, HttpServletResponse response) throws IOException {
        List<StatRiskExcel> list = statExportService.exportRisk(year);
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = System.currentTimeMillis() + "";
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        EasyExcel.write(response.getOutputStream())
                .head(statExportService.getRiskHead(year))
                .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) // 自定义列宽
                .sheet("数据") // 设置sheet名
                .doWrite(list); // 写入数据列表
    }

    @PostMapping(value = "/exportCz")
    public void exportCz(Integer year, HttpServletResponse response) throws IOException {
        List<StatCzExcel> list = statExportService.exportCz(year);
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = System.currentTimeMillis() + "";
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        EasyExcel.write(response.getOutputStream(), StatCzExcel.class).sheet("数据").doWrite(list);
    }

    @GetMapping(value = "/statRiskWord")
    public AjaxResult statRiskWord() {
        return success(statRiskService.riskWord());
    }

    @GetMapping(value = "/statRiskBar")
    public AjaxResult statRiskBar(Integer year,Boolean all) {
        return success(statRiskService.statRiskBar(year,all));
    }

    @GetMapping(value = "/statCostBar")
    public AjaxResult statCostBar(Integer year) {
        return success(statRiskService.statCostBar(year));
    }

    @GetMapping(value = "/statAuditNumBar")
    public AjaxResult statAuditNumBar(Integer year) {
        return success(statAuditService.statAuditNumBar(year));
    }

    @GetMapping(value = "/statAuditAmountBar")
    public AjaxResult statAuditAmountBar(Integer year) {
        return success(statAuditService.statAuditAmountBar(year));
    }

    @GetMapping(value = "/statSectorBar")
    public AjaxResult statSectorBar(Integer year) {
        return success(statProjectService.statSectorBar(year));
    }

    @GetMapping(value = "/statProject")
    public AjaxResult statProject(StatReq req) {
        return success(statProjectService.statProject(req));
    }

    @GetMapping(value = "/statAmount")
    public AjaxResult statAmount(StatReq req) {
        return success(statProjectService.statAmount(req));
    }

    @GetMapping(value = "/statTopic")
    public AjaxResult statTopic() {
        return success(statProjectService.statTopic());
    }

    @GetMapping(value = "/statCzBar")
    public AjaxResult statMonthBar(Integer year,Boolean outFlag) {
        return success(statProjectService.statCzLine(year,outFlag));
    }

    @GetMapping(value = "/statYjBar")
    public AjaxResult statYjBar(Integer year,Boolean outFlag) {
        return success(statProjectService.statYjLine(year,outFlag));
    }

    @GetMapping(value = "/statDeptBar")
    public AjaxResult statDeptBar(Integer year,Boolean outFlag) {
        return success(statProjectService.statDeptBar(year,outFlag));
    }

    @GetMapping(value = "/getTopicList")
    public AjaxResult getTopicList(Integer year, Integer month,String type) {
        return success(calendarService.getTopicList(year, month,type));
    }
}
